﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;


public class NumToWords
{

    #region IStringOperation Members

    /// ········
    /// Converts the given numerci amount to Words········
    /// ········
    /// Amount to be conveted········
    /// ········
    public string ToWords(double Rupees,string type)
    {
        return ToWords(Rupees, true, type);
    }    
    /// ········
    /// Converts the given numerci amount to Words········
    /// ········
    /// Amount to be converted········
    /// True/False for the string Ruppes Only has ········
    /// to be attached to the converted amount or not········
    /// ········
    string CurrencyType = "";
    public string ToWords(double Rupees, bool AttachRupeesOnly,string type)
    {
        string Crore, Lakh, Thousand, Hundred, TensUnits, Paisa, strRupees;
        ToWords(Rupees, out Crore, out Lakh, out Thousand, out Hundred, out TensUnits, out Paisa,type);
        strRupees = Crore + Lakh + Thousand + Hundred + TensUnits + Paisa;
        if(type == "INR")
        {
            CurrencyType = "Rupees";
        }
        else if (type == "EUR")
        {
            CurrencyType = "EURO";
        }
        else if (type == "GBP")
        {
            CurrencyType = "GBP";
        }
        else if (type == "USD")
        {
            CurrencyType = "USD";
        }
        return (AttachRupeesOnly ? CurrencyType + " " + strRupees + " Only" : strRupees);
    }
    /// ········
    /// Returns string in rupees in words format of given reupees value········
    /// Usign Mid(string, int, int), Overloaded Mid(string, int); Methods of clsSupport.cs class········
    /// Using System.Math.Abs(double); Methods of System.Math class········
    /// Using FnStrDigits(string, string, string); Methods of this calss········
    /// ········
    /// ········
    /// string········
    public void ToWords(double Rupees, out string Crore, out string Lakh, out string Thousand, out string Hundred, out string TensUnits, out string Paisa,string type)
    {
        Crore = Lakh = Thousand = Hundred = TensUnits = Paisa = string.Empty;
        Rupees = Math.Abs(Rupees);
        if (Rupees == 0)
        {
            TensUnits = "Zero";
        }
        else
        {
            string Rupee_Str = String.Format("{0:0.00}", Convert.ToDouble(Rupees));
            string strPaise = Rupee_Str.Substring(Rupee_Str.IndexOf(".") + 1, 2);
            Rupee_Str = String.Format("{0:000000000000000}", Convert.ToDouble(Rupee_Str.Substring(0, Rupee_Str.IndexOf("."))));
            double Crores = Convert.ToDouble(Rupee_Str.Substring(0, Rupee_Str.Length - 7));

            Crore = Crores > 99 ? ToWords(Crores, false,type) + " Crore " :
            JoinDigits(string.Empty, " Crore ", Crores.ToString());

            Lakh = JoinDigits(string.Empty, " Lakhs ", Rupee_Str.Substring(Rupee_Str.Length - 7, 2));
            Thousand = JoinDigits(string.Empty, " Thousand ", Rupee_Str.Substring(Rupee_Str.Length - 5, 2));
            Hundred = JoinDigits(string.Empty, " Hundred ", Rupee_Str.Substring(Rupee_Str.Length - 3, 1));
            TensUnits = JoinDigits(string.Empty, string.Empty, Rupee_Str.Substring(Rupee_Str.Length - 2, 2));
            if (type != "INR")
            {
                Paisa = strPaise.Length == 0 ? string.Empty : JoinDigits(" and ", " Cents", Convert.ToDouble(strPaise).ToString());
            }
            else
            {
                Paisa = strPaise.Length == 0 ? string.Empty : JoinDigits(" and ", " Paise", Convert.ToDouble(strPaise).ToString());
            }
        }
    }
    /// ········
    /// Returns string equivalent to given rupees value········
    /// Using ConvertDigit(int); Methods of this class········
    /// ········
    /// ········
    /// ········
    /// ········
    /// string········
    private string JoinDigits(string PreText, string PostText, string Dig_Val)
    {
        Int32 Amount = Convert.ToInt32(Dig_Val);
        return (Amount == 0) ? string.Empty : (PreText + ConvertDigit(Amount) + PostText);
    }
    /// ········
    /// Returns string equivalent to number.········
    /// ········
    /// ········
    /// string········
    public static string ConvertDigit(int Amount)
    {
        string[] AmountToWords = { string.Empty, "One", "Two", "Three", "Four", "Five", "Six"
            ,"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", 
            "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Thirty", "Forty", "Fifty"
            , "Sixty", "Seventy", "Eighty", "Ninety" };

        return Amount > 20 ? AmountToWords[20 + Amount / 10 - 2] + " " + AmountToWords[Amount % 10]
        : AmountToWords[Amount];
    }
    #endregion····
}
